package com.javabasic.algorithm.leetcode;

/**
 * @author mir.xiong
 * @version 1.0
 * @description
 * @see [2310. 个位数字为 K 的整数之和 - 力扣（LeetCode）](https://leetcode.cn/problems/sum-of-numbers-with-units-digit-k/)
 * @since Created by work on 2022/6/20 20:42
 */
public class SumOfNumbersWithUnitsDigitK {

    /**
     * 贪心算法：(n*10+unit[各位数字]) % k == 0 即为最少的个数
     * @param num
     * @param k
     * @return
     */
    public int minimumNumbers(int num, int k) {
        if (num == 0) {
            return 0;
        }
        int unit = num % 10;
        if (k == 0) {
            if (unit == 0) {
                return 1;
            } else {
                return -1;
            }
        }
        if (unit != 0 && unit%k == 0) {
            return unit/k;
        }

        int diff = 10;
        while (num >= (diff+unit) && diff <= 100) {
            if ((diff+unit) % k == 0) {
                return (diff+unit) / k;
            }
            diff += 10;
        }

        return -1;
    }
}
